package com.wm.ad_navi_new.custom

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.core.widget.NestedScrollView

class SettingNestedScrollView : NestedScrollView {


    constructor(context: Context) : this(context,null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var mLastY = 0

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {

        // 手指当前的位置
        val y: Int = ev.y.toInt()

        when(ev.action) {
            MotionEvent.ACTION_DOWN -> {
                mLastY = y
                parent.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE -> {
                val dy = mLastY - y

                // 向上滑动
                if (dy > 16) {
                    if (canScrollVertically(1)) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                }

                // 向下滑动
                if (dy < 0) {
                    if (canScrollVertically(-1)) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
            }

        }
        return super.dispatchTouchEvent(ev)
    }

}